﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using DevComponents.DotNetBar;
using PlanningGIS.Framework;
using PlanningGIS.Win.Controls;
using PlanningGIS.ArcGIS;
using PlanningGIS.Engine.Controls;

namespace PlanningGIS.TocContextMenu.Button
{
    /// <summary>
    /// 作用：打开属性表
    /// </summary>
    public class OpenAttributeTable : BaseCommand
    {
        /// <summary>
        /// 应用程序
        /// </summary>
        IApplication _Application = null;
        
        /// <summary>
        /// 构造函数
        /// </summary>
        public OpenAttributeTable()
        {
            base.m_caption = "打开属性表";
            base.m_toolTip = base.Caption;
        }

        /// <summary>
        /// 创建按钮
        /// </summary>
        /// <param name="hook"></param>
        public override void OnCreate(object hook)
        {
            if (_Application == null)
            {
                _Application = hook as IApplication;
            }
        }
        
        /// <summary>
        /// 按钮点击事件
        /// </summary>
        public override void OnClick()
        {
            if (_Application == null) return;
            if (!((_Application as IFramework).TOCControl.CustomProperty is IFeatureLayer)) return;
            IFeatureLayer featlyr = (_Application as IFramework).TOCControl.CustomProperty as IFeatureLayer;
            if (featlyr == null || !featlyr.Valid || featlyr.FeatureClass == null) return;
            object hook = (_Application as IFramework).MapControl.Object;
            Control ctl = FrameworkHelper.GetDockPanelControl(FrameworkHelper._DockpanelAttributeName);
            if (ctl == null)
            {
                ctl = new CtlAttributeTable(hook);
                FrameworkHelper.AddDockPanel(FrameworkHelper._DockpanelAttributeName, ctl, true, esriDockFlags.esriDockBottom);
            }
            (ctl as CtlAttributeTable).SetAttributes(featlyr);
            FrameworkHelper.ActivateDockPanel(FrameworkHelper._DockpanelAttributeName);
        }
        
        /// <summary>
        /// 
        /// </summary>
        public override int Bitmap
        {
            get
            {
                string bitmapResourceName = "PlanningGIS.TocContextMenu.Res." + this.GetType().Name + ".ico";
                System.IO.Stream stream = GetType().Assembly.GetManifestResourceStream(bitmapResourceName);
                if (stream == null) return 0;
                return new Bitmap(stream).GetHbitmap().ToInt32();
            }
        }
    }

}
